package lang

import (
	"crypto/md5"
	"fmt"
	"io"
	"sort"
	"strings"
	"time"
)

func mainMd5() {
	appid := "00000022"
	token := "77f64d1eef4549e78d0b1d0d01dd73e3"
	md5key := "860508ace30b4619be8e31623ed41ed0"
	t := time.Now()
	formatted := fmt.Sprintf("%d-%02d-%02d %02d:%02d:%02d:%03d",
		t.Year(), t.Month(), t.Day(),
		t.Hour(), t.Minute(), t.Second(), t.Nanosecond()/1000000)
	var parameterMap map[string]string = map[string]string{
		"appid":     appid,
		"version":   "1.0.0",
		"timestamp": formatted,
		"token":     token,
		"quanid":    "",
		"quanname":  "",
		"typeid":    "",
		"type":      "",
		"isgroup":   "Y",
		"state":     "Y",
		"pageindex": "1",
		"pagesize":  "51",
	}

	keys := make([]string, 0, len(parameterMap))
	for k := range parameterMap {
		keys = append(keys, k)
	}

	sort.Strings(keys)
	data := ""
	for _, k := range keys {
		data += k + "=" + parameterMap[k] + "&"
	}
	data += md5key

	h := md5.New()
	fmt.Println(data)
	io.WriteString(h, data)

	value := strings.ToUpper(fmt.Sprintf("%x", h.Sum([]byte(nil))))
	fmt.Println(value)
}
